Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 646)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.04583 13.04157 13.03740 13.03330 13.02928 13.02533 13.02145 13.01763
## [9] 13.01386 13.01014 13.00647 13.00284 12.99924 12.99567 12.99212 12.98860
## [17] 12.98509 12.98159 12.97810 12.97460 12.97110 12.96759 12.96406 12.96051
## [25] 12.95694 12.95333 12.94969 12.94601 12.94228 12.93852 12.93476 12.93100
## [33] 12.92724 12.92349 12.91975 12.91601 12.91230 12.90860 12.90493 12.90128
## [41] 12.89766 12.89408 12.89053 12.88702 12.88355 12.88012 12.87675 12.87343
## [49] 12.87017 12.86696 12.86382 12.86074 12.85773 12.85480 12.85194 12.84916
## [57] 12.84646 12.84385 12.84132 12.83889 12.83656 12.83433 12.83219 12.83017
## [65] 12.82818 12.82616 12.82411 12.82205 12.81997 12.81788 12.81580 12.81371
## [73] 12.81164 12.80959 12.80756 12.80555 12.80358 12.80165 12.79977 12.79794
## [81] 12.79616 12.79445 12.79282 12.79125 12.78977 12.78838 12.78708 12.78588
## [89] 12.78478 12.78380 12.78293 12.78219 12.78157 12.78109 12.78076 12.78057
## [97] 12.78053 12.78065 12.78094 12.78128 12.78158 12.78185 12.78208 12.78229
## [105] 12.78249 12.78268 12.78286 12.78306 12.78327 12.78350 12.78376 12.78405
## [113] 12.78439 12.78479 12.78524 12.78576 12.78635 12.78702 12.78778 12.78864
## [121] 12.78960 12.79067 12.79186 12.79318 12.79462 12.79621 12.79795 12.79984
## [129] 12.80190 12.80413 12.80653 12.80912 12.81190 12.81588 12.82187 12.82960
## [137] 12.83880 12.84920 12.86053 12.87252 12.88488 12.89736 12.90969 12.92158
## [145] 12.93277 12.94299 12.95196 12.95942 12.96508 12.97117 12.97986 12.99085
## [153] 13.00383 13.01850 13.03455 13.05167 13.06955 13.08791 13.10641 13.12477
## [161] 13.14268 13.15982 13.17590 13.19060 13.20363 13.21467 13.22343 13.22958
## [169] 13.23480 13.24087 13.24771 13.25521 13.26330 13.27186 13.28082 13.29007
## [177] 13.29953 13.30909 13.31867 13.32818 13.33752 13.34659 13.35531 13.36358
## [185] 13.37130 13.37839 13.38475 13.39029 13.39491 13.39853 13.40104 13.40235
## [193] 13.40238 13.40102 13.39819 13.39379 13.38787 13.38059 13.37209 13.36248
## [201] 13.35189 13.34043 13.32823 13.31540 13.30206 13.28833 13.27434 13.26020
## [209] 13.24604 13.23197 13.21811 13.20458 13.18938 13.17065 13.14878 13.12415
## [217] 13.09714 13.06814 13.03751 13.00564 12.97291 12.93971 12.90641 12.87339
## [225] 12.84103 12.80972 12.77983 12.75174 12.72584 12.70250 12.68211 12.66151
## [233] 12.63762 12.61092 12.58189 12.55100 12.51874 12.48558 12.45201 12.41851
## [241] 12.38556 12.35363 12.32320 12.29476 12.26879 12.24576 12.22616 12.20874
## [249] 12.19192 12.17567 12.15995 12.14472 12.12996 12.11562 12.10166 12.08807
## [257] 12.07478 12.06179 12.04904 12.03650 12.02414 12.01191 11.99980 11.98775
## [265] 11.97574 11.96373 11.95318 11.94534 11.93985 11.93636 11.93451 11.93394
## [273] 11.93431 11.93524 11.93640 11.93741 11.93793 11.93760 11.93606 11.93296
## [281] 11.92795 11.92066 11.91266 11.90567 11.89958 11.89427 11.88962 11.88551
## [289] 11.88182 11.87844 11.87524 11.87211 11.86893 11.86559 11.86196 11.85793
## [297] 11.85337 11.84817 11.84222 11.83539 11.82756 11.81866 11.80879 11.79808
## [305] 11.78670 11.77477 11.76246 11.74991 11.73726 11.72467 11.71227 11.70022
## [313] 11.68866 11.67774 11.66761 11.65841 11.65028 11.64129 11.62958 11.61551
## [321] 11.59940 11.58159 11.56241 11.54220 11.52129 11.50002 11.47872 11.45773
## [329] 11.43738 11.41801 11.39994 11.38353 11.36909 11.35697 11.34750 11.34101
## [337] 11.33785 11.33558 11.33180 11.32680 11.32094 11.31451 11.30786 11.30129
## [345] 11.29514 11.28973 11.28537 11.28240 11.28113 11.28190 11.28501 11.29080
## [353] 11.29942 11.31063 11.32423 11.33998 11.35766 11.37703 11.39789 11.41999
## [361] 11.44312 11.46705 11.49154 11.51639 11.54135 11.56621 11.59074 11.61471
## [369] 11.63790 11.66008 11.68102 11.70051 11.72142 11.74652 11.77533 11.80740
## [377] 11.84229 11.87952 11.91865 11.95923 12.00078 12.04287 12.08502 12.12680
## [385] 12.16774 12.20738 12.24527 12.28095 12.31398 12.34388 12.37021 12.39252
## [393] 12.41433 12.43916 12.46652 12.49590 12.52682 12.55878 12.59128 12.62383
## [401] 12.65594 12.68711 12.71684 12.74463 12.77001 12.79246 12.81149 12.82662
## [409] 12.83931 12.85138 12.86283 12.87366 12.88386 12.89343 12.90238 12.91069
## [417] 12.91838 12.92543 12.93185 12.93764 12.94279 12.94730 12.95117 12.95440
## [425] 12.95699 12.95894 12.96024 12.95924 12.95460 12.94676 12.93618 12.92332
## [433] 12.90863 12.89256 12.87558 12.85813 12.84067 12.82365 12.80753 12.79277
## [441] 12.77981 12.76912 12.76115 12.75327 12.74275 12.72989 12.71500 12.69838
## [449] 12.68031 12.66111 12.64107 12.62050 12.59969 12.57894 12.55856 12.53884
## [457] 12.52008 12.50259 12.48666 12.47260 12.46069 12.45126 12.44188 12.43020
## [465] 12.41657 12.40134 12.38485 12.36746 12.34952 12.33138 12.31339 12.29591
## [473] 12.27928 12.26385 12.24998 12.23802 12.22831 12.22121 12.21540 12.20937
## [481] 12.20321 12.19698 12.19075 12.18460 12.17860 12.17282 12.16734 12.16223
## [489] 12.15755 12.15340 12.14982 12.14691 12.14473 12.14336 12.14286 12.14331
## [497] 12.14478 12.14805 12.15367 12.16142 12.17108 12.18242 12.19522 12.20925
## [505] 12.22429 12.24011 12.25649 12.27321 12.29003 12.30673 12.32309 12.33889
## [513] 12.35390 12.36789 12.38064 12.39192 12.40151 12.40919 12.41788 12.43029
## [521] 12.44586 12.46404 12.48426 12.50598 12.52862 12.55164 12.57448 12.59658
## [529] 12.61738 12.63633 12.65286 12.66642 12.67646 12.68241 12.68645 12.69107
## [537] 12.69619 12.70171 12.70756 12.71365 12.71990 12.72621 12.73250 12.73870
## [545] 12.74470 12.75044 12.75581 12.76075 12.76515 12.76895 12.77204 12.77435
## [553] 12.77580 12.77629 12.77573 12.77406 12.77117 12.76699 12.76143 12.75441
## [561] 12.74472 12.73156 12.71537 12.69663 12.67580 12.65334 12.62973 12.60542
## [569] 12.58087 12.55656 12.53295 12.51050 12.48968 12.47095 12.45477 12.44161
## [577] 12.42963 12.41670 12.40293 12.38841 12.37323 12.35748 12.34126 12.32466
## [585] 12.30776 12.29067 12.27347 12.25627 12.23914 12.22218 12.20548 12.18914
## [593] 12.17326 12.15791 12.14319 12.12857 12.11348 12.09798 12.08211 12.06594
## [601] 12.04953 12.03291 12.01616 11.99933 11.98247 11.96563 11.94888 11.93226
## [609] 11.91584 11.89966 11.88355 11.86728 11.85086 11.83431 11.81764 11.80085
## [617] 11.78396 11.76698 11.74991 11.73278 11.71558 11.69834 11.68105 11.66374
## [625] 11.64641 11.62907 11.61174 11.59442 11.57712 11.55986 11.54281 11.52610
## [633] 11.50970 11.49358 11.47768 11.46197 11.44642 11.43098 11.41561 11.40027
## [641] 11.38493 11.36955 11.35408 11.33849 11.32273 11.30678
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 646)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.63140 12.62652 12.62174 12.61706 12.61248 12.60799 12.60360 12.59931
## [9] 12.59510 12.59099 12.58697 12.58304 12.57920 12.57544 12.57176 12.56818
## [17] 12.56467 12.56124 12.55790 12.55463 12.55144 12.54832 12.54528 12.54231
## [25] 12.53941 12.53659 12.53383 12.53114 12.52852 12.52596 12.52346 12.52103
## [33] 12.51866 12.51635 12.51409 12.51190 12.50975 12.50765 12.50560 12.50360
## [41] 12.50166 12.49977 12.49795 12.49619 12.49450 12.49288 12.49132 12.48985
## [49] 12.48845 12.48713 12.48590 12.48476 12.48370 12.48274 12.48187 12.48110
## [57] 12.48043 12.47986 12.47940 12.47905 12.47882 12.47869 12.47869 12.47881
## [65] 12.47905 12.47942 12.47992 12.48055 12.48131 12.48222 12.48326 12.48446
## [73] 12.48582 12.48733 12.48899 12.49080 12.49275 12.49485 12.49708 12.49945
## [81] 12.50195 12.50457 12.50732 12.51019 12.51318 12.51628 12.51949 12.52280
## [89] 12.52622 12.52974 12.53336 12.53707 12.54086 12.54475 12.54871 12.55276
## [97] 12.55688 12.56107 12.56533 12.56966 12.57405 12.57849 12.58299 12.58755
## [105] 12.59215 12.59679 12.60156 12.60652 12.61166 12.61697 12.62243 12.62803
## [113] 12.63376 12.63961 12.64555 12.65159 12.65770 12.66387 12.67009 12.67635
## [121] 12.68263 12.68892 12.69521 12.70148 12.70771 12.71391 12.72005 12.72612
## [129] 12.73211 12.73800 12.74379 12.74945 12.75498 12.76128 12.76914 12.77834
## [137] 12.78866 12.79988 12.81177 12.82413 12.83673 12.84936 12.86178 12.87380
## [145] 12.88518 12.89572 12.90518 12.91335 12.92002 12.92752 12.93812 12.95143
## [153] 12.96710 12.98477 13.00406 13.02461 13.04606 13.06804 13.09018 13.11212
## [161] 13.13349 13.15393 13.17307 13.19055 13.20600 13.21905 13.22935 13.23652
## [169] 13.24236 13.24887 13.25596 13.26354 13.27153 13.27984 13.28838 13.29706
## [177] 13.30580 13.31451 13.32310 13.33149 13.33959 13.34732 13.35457 13.36128
## [185] 13.36735 13.37270 13.37723 13.38086 13.38351 13.38509 13.38550 13.38467
## [193] 13.38251 13.37892 13.37383 13.36714 13.35782 13.34515 13.32955 13.31142
## [201] 13.29117 13.26921 13.24595 13.22179 13.19716 13.17245 13.14807 13.12444
## [209] 13.10197 13.08105 13.06210 13.04554 13.02829 13.00732 12.98305 12.95592
## [217] 12.92635 12.89477 12.86160 12.82729 12.79225 12.75693 12.72173 12.68710
## [225] 12.65347 12.62126 12.59090 12.56282 12.53745 12.51522 12.49656 12.47878
## [233] 12.45913 12.43790 12.41538 12.39188 12.36769 12.34310 12.31841 12.29392
## [241] 12.26993 12.24673 12.22462 12.20390 12.18485 12.16779 12.15300 12.14015
## [249] 12.12862 12.11831 12.10909 12.10085 12.09347 12.08683 12.08083 12.07534
## [257] 12.07025 12.06544 12.06079 12.05620 12.05154 12.04670 12.04156 12.03600
## [265] 12.02992 12.02319 12.01806 12.01653 12.01810 12.02229 12.02858 12.03649
## [273] 12.04552 12.05518 12.06497 12.07440 12.08297 12.09019 12.09555 12.09858
## [281] 12.09876 12.09561 12.09127 12.08813 12.08604 12.08484 12.08438 12.08450
## [289] 12.08506 12.08590 12.08687 12.08781 12.08857 12.08900 12.08895 12.08826
## [297] 12.08679 12.08437 12.08085 12.07609 12.06993 12.06195 12.05201 12.04037
## [305] 12.02730 12.01306 11.99790 11.98210 11.96590 11.94958 11.93340 11.91761
## [313] 11.90249 11.88828 11.87526 11.86368 11.85381 11.84310 11.82909 11.81219
## [321] 11.79279 11.77127 11.74804 11.72348 11.69800 11.67199 11.64583 11.61993
## [329] 11.59468 11.57047 11.54770 11.52676 11.50805 11.49195 11.47888 11.46921
## [337] 11.46334 11.45861 11.45233 11.44481 11.43639 11.42741 11.41819 11.40906
## [345] 11.40036 11.39242 11.38556 11.38012 11.37643 11.37482 11.37562 11.37917
## [353] 11.38560 11.39468 11.40618 11.41986 11.43550 11.45286 11.47171 11.49182
## [361] 11.51294 11.53486 11.55733 11.58013 11.60302 11.62577 11.64815 11.66992
## [369] 11.69085 11.71071 11.72927 11.74629 11.76444 11.78628 11.81141 11.83945
## [377] 11.86999 11.90264 11.93700 11.97269 12.00931 12.04646 12.08374 12.12078
## [385] 12.15716 12.19249 12.22639 12.25846 12.28829 12.31551 12.33970 12.36049
## [393] 12.38089 12.40391 12.42913 12.45612 12.48448 12.51377 12.54357 12.57346
## [401] 12.60303 12.63184 12.65947 12.68551 12.70954 12.73112 12.74985 12.76529
## [409] 12.77926 12.79376 12.80866 12.82383 12.83914 12.85444 12.86961 12.88452
## [417] 12.89903 12.91300 12.92631 12.93883 12.95041 12.96093 12.97025 12.97825
## [425] 12.98478 12.98971 12.99292 12.99381 12.99209 12.98804 12.98195 12.97411
## [433] 12.96481 12.95434 12.94298 12.93101 12.91874 12.90645 12.89442 12.88295
## [441] 12.87232 12.86282 12.85474 12.84540 12.83223 12.81567 12.79616 12.77414
## [449] 12.75005 12.72432 12.69740 12.66972 12.64172 12.61384 12.58652 12.56020
## [457] 12.53532 12.51231 12.49162 12.47368 12.45893 12.44782 12.43705 12.42336
## [465] 12.40720 12.38902 12.36926 12.34839 12.32684 12.30507 12.28353 12.26267
## [473] 12.24294 12.22479 12.20867 12.19503 12.18432 12.17700 12.17117 12.16476
## [481] 12.15787 12.15063 12.14317 12.13560 12.12805 12.12064 12.11350 12.10674
## [489] 12.10049 12.09488 12.09002 12.08603 12.08305 12.08119 12.08057 12.08132
## [497] 12.08356 12.08781 12.09434 12.10295 12.11345 12.12562 12.13925 12.15415
## [505] 12.17011 12.18693 12.20439 12.22229 12.24043 12.25860 12.27660 12.29422
## [513] 12.31125 12.32750 12.34275 12.35681 12.36946 12.38050 12.39378 12.41275
## [521] 12.43659 12.46447 12.49557 12.52907 12.56414 12.59997 12.63574 12.67061
## [529] 12.70378 12.73441 12.76168 12.78478 12.80288 12.81516 12.82495 12.83601
## [537] 12.84819 12.86133 12.87529 12.88991 12.90504 12.92053 12.93621 12.95194
## [545] 12.96757 12.98294 12.99790 13.01230 13.02597 13.03878 13.05056 13.06117
## [553] 13.07044 13.07823 13.08438 13.08875 13.09117 13.09149 13.08957 13.08524
## [561] 13.07782 13.06702 13.05323 13.03689 13.01840 12.99819 12.97665 12.95422
## [569] 12.93131 12.90833 12.88569 12.86382 12.84313 12.82403 12.80694 12.79227
## [577] 12.77839 12.76342 12.74744 12.73054 12.71279 12.69427 12.67506 12.65524
## [585] 12.63489 12.61408 12.59289 12.57141 12.54970 12.52786 12.50595 12.48406
## [593] 12.46227 12.44065 12.41928 12.39771 12.37547 12.35258 12.32911 12.30510
## [601] 12.28059 12.25563 12.23026 12.20453 12.17848 12.15217 12.12563 12.09892
## [609] 12.07207 12.04513 12.01795 11.99035 11.96234 11.93392 11.90511 11.87591
## [617] 11.84633 11.81639 11.78608 11.75543 11.72443 11.69311 11.66145 11.62949
## [625] 11.59722 11.56465 11.53179 11.49866 11.46525 11.43159 11.39781 11.36402
## [633] 11.33019 11.29630 11.26230 11.22818 11.19388 11.15938 11.12466 11.08967
## [641] 11.05439 11.01877 10.98280 10.94643 10.90964 10.87239
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 646)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 12.04946 12.04330 12.03728 12.03138 12.02561 12.01995 12.01440 12.00895
## [9] 12.00361 11.99836 11.99321 11.98814 11.98315 11.97824 11.97341 11.96864
## [17] 11.96393 11.95928 11.95468 11.95014 11.94563 11.94116 11.93673 11.93232
## [25] 11.92794 11.92358 11.91923 11.91489 11.91056 11.90622 11.90188 11.89753
## [33] 11.89317 11.88878 11.88437 11.87993 11.87550 11.87110 11.86675 11.86245
## [41] 11.85820 11.85401 11.84987 11.84579 11.84179 11.83784 11.83398 11.83018
## [49] 11.82647 11.82284 11.81929 11.81583 11.81247 11.80920 11.80603 11.80296
## [57] 11.80000 11.79715 11.79441 11.79178 11.78928 11.78690 11.78464 11.78252
## [65] 11.78053 11.77867 11.77696 11.77538 11.77396 11.77268 11.77156 11.77045
## [73] 11.76922 11.76787 11.76643 11.76491 11.76333 11.76169 11.76002 11.75833
## [81] 11.75663 11.75493 11.75326 11.75163 11.75005 11.74853 11.74710 11.74576
## [89] 11.74453 11.74342 11.74246 11.74165 11.74101 11.74055 11.74029 11.74024
## [97] 11.74042 11.74085 11.74153 11.74248 11.74371 11.74525 11.74710 11.74929
## [105] 11.75181 11.75470 11.75787 11.76124 11.76481 11.76858 11.77255 11.77672
## [113] 11.78110 11.78569 11.79048 11.79547 11.80068 11.80609 11.81171 11.81755
## [121] 11.82359 11.82985 11.83632 11.84301 11.84991 11.85703 11.86437 11.87192
## [129] 11.87970 11.88770 11.89592 11.90436 11.91302 11.92353 11.93721 11.95364
## [137] 11.97240 11.99307 12.01522 12.03843 12.06228 12.08635 12.11022 12.13346
## [145] 12.15564 12.17636 12.19518 12.21169 12.22545 12.23983 12.25814 12.27992
## [153] 12.30471 12.33203 12.36144 12.39245 12.42462 12.45747 12.49055 12.52338
## [161] 12.55551 12.58647 12.61579 12.64302 12.66769 12.68933 12.70748 12.72168
## [169] 12.73420 12.74756 12.76165 12.77635 12.79156 12.80716 12.82305 12.83911
## [177] 12.85523 12.87130 12.88721 12.90285 12.91811 12.93287 12.94703 12.96048
## [185] 12.97311 12.98479 12.99543 13.00492 13.01313 13.01997 13.02532 13.02906
## [193] 13.03110 13.03131 13.02959 13.02583 13.01913 13.00895 12.99570 12.97980
## [201] 12.96165 12.94167 12.92026 12.89783 12.87481 12.85159 12.82859 12.80622
## [209] 12.78489 12.76502 12.74700 12.73126 12.71443 12.69322 12.66811 12.63961
## [217] 12.60820 12.57438 12.53866 12.50151 12.46345 12.42496 12.38654 12.34869
## [225] 12.31189 12.27665 12.24347 12.21282 12.18522 12.16116 12.14113 12.12225
## [233] 12.10148 12.07912 12.05544 12.03073 12.00529 11.97938 11.95329 11.92731
## [241] 11.90173 11.87683 11.85288 11.83019 11.80902 11.78967 11.77242 11.75677
## [249] 11.74197 11.72795 11.71464 11.70197 11.68986 11.67825 11.66706 11.65622
## [257] 11.64566 11.63530 11.62508 11.61492 11.60475 11.59450 11.58410 11.57347
## [265] 11.56254 11.55124 11.54103 11.53319 11.52741 11.52337 11.52078 11.51930
## [273] 11.51864 11.51847 11.51849 11.51839 11.51784 11.51655 11.51419 11.51045
## [281] 11.50503 11.49760 11.48966 11.48283 11.47699 11.47201 11.46779 11.46421
## [289] 11.46114 11.45847 11.45609 11.45387 11.45170 11.44946 11.44703 11.44430
## [297] 11.44114 11.43744 11.43309 11.42796 11.42194 11.41437 11.40487 11.39371
## [305] 11.38117 11.36755 11.35311 11.33814 11.32293 11.30775 11.29288 11.27860
## [313] 11.26521 11.25297 11.24217 11.23309 11.22601 11.21857 11.20845 11.19599
## [321] 11.18151 11.16535 11.14785 11.12932 11.11011 11.09055 11.07097 11.05170
## [329] 11.03308 11.01544 10.99911 10.98442 10.97171 10.96130 10.95354 10.94875
## [337] 10.94727 10.94787 10.94909 10.95094 10.95341 10.95650 10.96021 10.96454
## [345] 10.96948 10.97503 10.98119 10.98796 10.99534 11.00331 11.01189 11.02107
## [353] 11.03206 11.04589 11.06230 11.08101 11.10177 11.12430 11.14834 11.17361
## [361] 11.19987 11.22682 11.25422 11.28179 11.30926 11.33637 11.36284 11.38843
## [369] 11.41284 11.43583 11.45712 11.47644 11.49618 11.51870 11.54369 11.57084
## [377] 11.59984 11.63038 11.66215 11.69483 11.72813 11.76172 11.79531 11.82857
## [385] 11.86121 11.89290 11.92335 11.95224 11.97925 12.00409 12.02644 12.04599
## [393] 12.06496 12.08558 12.10756 12.13062 12.15445 12.17878 12.20330 12.22772
## [401] 12.25177 12.27514 12.29754 12.31868 12.33828 12.35604 12.37166 12.38487
## [409] 12.39674 12.40853 12.42018 12.43166 12.44293 12.45392 12.46461 12.47494
## [417] 12.48488 12.49436 12.50336 12.51182 12.51970 12.52696 12.53354 12.53941
## [425] 12.54452 12.54882 12.55227 12.55407 12.55361 12.55115 12.54695 12.54128
## [433] 12.53440 12.52656 12.51803 12.50907 12.49994 12.49089 12.48220 12.47411
## [441] 12.46690 12.46082 12.45614 12.45114 12.44412 12.43527 12.42483 12.41301
## [449] 12.40003 12.38610 12.37145 12.35630 12.34086 12.32535 12.30999 12.29500
## [457] 12.28060 12.26700 12.25443 12.24310 12.23323 12.22504 12.21649 12.20562
## [465] 12.19276 12.17825 12.16244 12.14565 12.12824 12.11054 12.09288 12.07561
## [473] 12.05907 12.04359 12.02951 12.01718 12.00693 11.99910 11.99189 11.98344
## [481] 11.97391 11.96351 11.95243 11.94085 11.92896 11.91696 11.90504 11.89338
## [489] 11.88218 11.87162 11.86190 11.85321 11.84573 11.83966 11.83519 11.83251
## [497] 11.83181 11.83284 11.83519 11.83874 11.84339 11.84904 11.85559 11.86294
## [505] 11.87099 11.87963 11.88876 11.89827 11.90808 11.91807 11.92814 11.93819
## [513] 11.94812 11.95782 11.96720 11.97616 11.98458 11.99237 12.00196 12.01549
## [521] 12.03239 12.05210 12.07406 12.09771 12.12248 12.14781 12.17314 12.19791
## [529] 12.22156 12.24352 12.26324 12.28014 12.29367 12.30327 12.31144 12.32097
## [537] 12.33172 12.34355 12.35633 12.36991 12.38415 12.39893 12.41408 12.42949
## [545] 12.44500 12.46048 12.47580 12.49080 12.50536 12.51932 12.53256 12.54494
## [553] 12.55631 12.56654 12.57548 12.58300 12.58896 12.59322 12.59564 12.59609
## [561] 12.59437 12.59059 12.58499 12.57780 12.56929 12.55969 12.54925 12.53822
## [569] 12.52684 12.51537 12.50404 12.49310 12.48280 12.47339 12.46511 12.45820
## [577] 12.45178 12.44478 12.43724 12.42917 12.42062 12.41161 12.40217 12.39231
## [585] 12.38208 12.37149 12.36058 12.34938 12.33790 12.32618 12.31425 12.30213
## [593] 12.28985 12.27744 12.26492 12.25212 12.23885 12.22512 12.21097 12.19642
## [601] 12.18148 12.16617 12.15053 12.13458 12.11832 12.10180 12.08503 12.06802
## [609] 12.05081 12.03342 12.01575 11.99772 11.97933 11.96058 11.94149 11.92206
## [617] 11.90231 11.88223 11.86184 11.84115 11.82016 11.79887 11.77731 11.75547
## [625] 11.73337 11.71100 11.68839 11.66554 11.64245 11.61913 11.59568 11.57216
## [633] 11.54857 11.52487 11.50104 11.47705 11.45290 11.42855 11.40399 11.37918
## [641] 11.35411 11.32876 11.30310 11.27712 11.25078 11.22407
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")